package com.lagou.rpc.provider.service;

import com.lagou.rpc.api.IUserService;
import com.lagou.rpc.pojo.User;
import com.lagou.rpc.provider.anno.RpcService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@RpcService
@Service
public class UserServiceImpl implements IUserService {
    List<User> userList = new ArrayList<>();

    @Override
    public User getById(int id) {
        if (userList.size() == 0) {
            User user1 = new User();
            user1.setId(1);
            user1.setName("张三");
            User user2 = new User();
            user2.setId(2);
            user2.setName("李四");
            userList.add(user1);
            userList.add(user2);
        }
        User user = userList.stream().filter(item -> id == item.getId()).findFirst().get();
        return user;
    }

    @Override
    public User getByName(String name) {
        if (userList.size() == 0) {
            User user1 = new User();
            user1.setId(1);
            user1.setName("张三");
            User user2 = new User();
            user2.setId(2);
            user2.setName("李四");
            userList.add(user1);
            userList.add(user2);
        }
        User user = userList.stream().filter(item -> item.getName().equals(name)).findFirst().get();
        return user;
    }
}